home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13230 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  82 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Dynamically initializing static members
  5. Date: 24 Mar 1996 12:23:34 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4j3es6$pd6@arl-news-svc-2.compuserve.com>
  8. NNTP-Posting-Host: dd22-055.compuserve.com
  9.  
  10. martijnl@xs4all.nl (Martijn Lievaart) s'Θcrit :
  11. > class MYCLASS {
  12. >   static unsigned int Registration_ID
  13. >   ...
  14. > }
  15. > unsigned int MYCLASS::Registration_D = Registrate("MYCLASS");
  16. > should do the trick (you where almost there!).
  17. > apardon@rc1.vub.ac.be (Antoon Pardon) wrote:
  18. > >I am playing with the idea of somekind of classregistration.
  19. > >The registration class would provide for a function: 
  20. > >unsigned int Registrate(const char *name);
  21. > >This would keep the name of the function stored somewhere and
  22. > >would return a registration ID.
  23. > >The idea would be to include a static int into the class that
  24. > >would store this ID something like this
  25. > >class MYCLASS {
  26. > >  static unsigned int
  27. > >    Registration_ID
  28. > >  ...
  29. > >}
  30. > >The problem now is how do I assign this Registration_ID
  31. > >automatically. In Modula 2 you have initialisation code
  32. > >that is called at the start of the program where you might
  33. > >then put code similar to 
  34. > >MYCLASS::Registration_D = Registrate("MYCLASS");
  35. > >Is something similar possible in C++ or do I have to
  36. > >do it in a different way? I looked at the FAQ but
  37. > >couldn't find an answer there.
  38. >      /~~~~~| /~~~~~| /~~~~~~|~~~~~\~~~~~\~~~~|~~~~~|   We now return to our
  39. >     /      |/      |/       |   o  |  o  |  |   +-|     regularly scheduled
  40. >    /   /|     /|   |   /|   |  ___/  ___/   |   +-|          flame-throwing
  41. > ../___/.|____/.|___|__/~|___|_|..|__|..|_____|_____|...martijnl@xs4all.nl..
  42. Your solution will work provided you implement a Registrator
  43. class to do that work.
  44. This Registrator class will have no instance but will only
  45. use static members, such as a registered class list, an ID
  46. generator, and the Registrate method.
  47. The generated ID can be an index within the vector<Registration>
  48. static member. The Registration simply stores the class name
  49. and may be other type informations.
  50. If you want to implement persistent objects, you should care of
  51. the order in which registration of classes occur.
  52. So your persistent stream should always store not only the ID
  53. (for efficiency) but also the map from name to ID, so that
  54. matching is possible when reloading later your persistent
  55. objects in future versions of your application, or in other
  56. programs.
  57. To facilitate this conversion, you can differentiate the global
  58. and running class ID from the streamed class ID, by providing
  59. a local map for these types (when loading your objects, find
  60. their type by name, then get its associated in-stream ID, and
  61. create a memory map between this read ID and the global ID,
  62. so that you can identify types coming from the input stream.
  63. Good luck !
  64.